package com.fiberhome.nas.navigation

import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.fiberhome.nas.feature.album.navigation.albumGraph
import com.fiberhome.nas.feature.auth.navigation.authGraph
import com.fiberhome.nas.feature.file.navigation.fileGraph
import com.fiberhome.nas.feature.main.navigation.mainGraph
import com.fiberhome.nas.feature.photo.navigation.photoGraph
import com.fiberhome.nas.feature.setting.navigation.settingGraph
import com.fiberhome.nas.feature.taskcenter.navigation.taskCenterGraph
import com.fiberhome.nas.navigation.routes.AuthRoutes
import kotlinx.coroutines.flow.collectLatest

/**
 * 应用导航宿主
 * 配置整个应用的导航图和动画
 */
@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun AppNavHost(
    navigator: AppNavigator,
    modifier: Modifier = Modifier
) {
    val navController = rememberNavController()

    // 监听导航事件
    LaunchedEffect(navController) {
        navigator.navigationEvents.collectLatest { event ->
            navController.handleNavigationEvent(event)
        }
    }

    SharedTransitionLayout {
        NavHost(
            navController = navController,
            startDestination = AuthRoutes.ACCOUNT_LOGIN,
            modifier = modifier,
            // 页面进入动画
            enterTransition = {
                slideIntoContainer(
                    AnimatedContentTransitionScope.SlideDirection.Left,
                    animationSpec = tween(300)
                )
            },
            // 页面退出动画
            exitTransition = {
                slideOutOfContainer(
                    AnimatedContentTransitionScope.SlideDirection.Left,
                    animationSpec = tween(300)
                )
            },
            // 返回时页面进入动画
            popEnterTransition = {
                slideIntoContainer(
                    AnimatedContentTransitionScope.SlideDirection.Right,
                    animationSpec = tween(300)
                )
            },
            // 返回时页面退出动画
            popExitTransition = {
                slideOutOfContainer(
                    AnimatedContentTransitionScope.SlideDirection.Right,
                    animationSpec = tween(300)
                )
            }
        ) {
            // 只调用模块级Graph函数，大大减少了冲突可能性
            mainGraph(
                navController,
                this@SharedTransitionLayout
            )
            authGraph(navController)
            photoGraph(navController)
            albumGraph(navController)
            fileGraph(navController)
            settingGraph(navController)
            taskCenterGraph(navController)
        }
    }
}